/**
 * 提示框
 * ibio的配色：0x58A6D7, 0xD8E4F4, 0x004080
 * ibio-develop
 * 2009-1-18 15:45
 */
package com.ibio8.components.helpers {
	import flash.display.DisplayObject;
	import flash.display.Stage;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.utils.Dictionary;
	import flash.utils.Timer;
	import flash.events.TimerEvent;
	/**
	 * 标准提示框。
	 * 
	 * <p>
	 * 仿照 windows XP 样式的标准提示框。
	 * </p>
	 */
	public class ToolTip {
		/** 与鼠标 x 之间的间隔(px)，默认为 10。 **/
		public static var MARGIN_X:Number = 10;
		/** 与鼠标 y 之间的间隔(px)，默认为 10。 **/
		public static var MARGIN_Y:Number = 10;
		/** 延迟显示时间(ms) **/
		public static var TIME_DELAY_NUM:Number = 1;
		/** 中间文字与边框的间隔(px) **/
		public static var  PADDING_NUM:Number = 4;
		/** @private **/
		protected const SINGLETON_MSG:String = "单件模式的 ToolTip 已经创建！";
		/** @private **/
		protected static var m_instance:ToolTip;
		/** @private **/
		protected var m_stage:Stage;
		/** @private **/
		protected var m_tipPanel:TextField;
		/** @private **/
		protected var m_currTarget:DisplayObject;
		/** @private **/
		protected var m_borderColor:uint = 0x000000;
		/** @private **/
		protected var m_bgColor:uint = 0xFFFFE1;
		/** @private **/
		protected var m_fontColor:uint = 0x000000;
		/** @private **/
		protected var m_timer:Timer;
		/** @private **/
		protected var m_maxWidth:Number = 400;
		/** @private **/
		protected var m_tipList:Dictionary;
		
		/**
		 * 构造函数。
		 * 
		 * @throws Error 单件的<code>ToolTip</code>被实例化多次
		 */
		function ToolTip() {
			if (m_instance != null) {
				throw new Error(SINGLETON_MSG);
			}
			m_timer = new Timer(TIME_DELAY_NUM * 1000, 1);
			m_timer.addEventListener(TimerEvent.TIMER_COMPLETE, onTimerHandler);
			m_tipList = new Dictionary();
		}
		
		/**
		 * 此类为单例模式。此方法用来返回此类唯一的实例。
		 */
		public static function getInstance():ToolTip {
			if(m_instance == null){
				m_instance = new ToolTip();
			}
			return m_instance;
		}
		
		/**
		 * 设置当前 swf 的 stage。
		 */
		public function set stage(value:Stage):void {
			if (value) {
				m_stage = value;
			}
		}
		
		/**
		 * 设置某个 MC 的提示框
		 * @param	target 	当前添加提示对象
		 * @param	content	需要提示的内容
		 */
		public function addTip(target:DisplayObject, content:String):void {
			//如果传递进来的 MC 已经在显示列表内，则自动获取当前的 stage
			//若没有，则需要手动获取当前的 stage
			if (!m_stage) {
				m_stage = target.stage;
			}
			//把当前的str保存起来
			m_tipList[target] = content;
			//把要显示的信息指向到这个按钮的事件上去
			target.addEventListener(MouseEvent.MOUSE_OVER, onMouseOverHandler);
			target.addEventListener(MouseEvent.MOUSE_OUT, onMouseOutHandler);
		}
		
		/**
		 * 删除某 MC 的提示框
		 * @param	target	当前要删除的对象
		 */
		public function removeTip(target:DisplayObject):void {
			if (m_tipList[target]) {
				target.removeEventListener(MouseEvent.MOUSE_OVER, onMouseOverHandler);
				target.removeEventListener(MouseEvent.MOUSE_OUT, onMouseOutHandler);
				delete m_tipList[target];
			}
		}
		
		/**
		 * 设置边框、背景以及字体颜色 
		 * @param	borderColor	边框颜色
		 * @param	bgColor		背景颜色
		 * @param	fontColor	字体颜色
		 */
		public function setColor(borderColor:uint, bgColor:uint, fontColor:uint):void {
			m_borderColor = borderColor;
			m_bgColor = bgColor;
			m_fontColor = fontColor;
		}
		
		/**
		 * 设置最大宽度，若超过此宽度，则自动换行
		 */
		public function set maxWidth(value:Number):void {
			m_maxWidth = uint(value);
		}
		
		/** @private **/
		protected function onMouseOverHandler(e:MouseEvent):void {
			m_currTarget = e.currentTarget as DisplayObject;
			clearTip();
			showTip();
		}
		
		/** @private **/
		protected function onMouseOutHandler(e:MouseEvent):void {
			clearTip();
		}
		
		/** @private **/
		protected function showTip():void{
			m_timer.start();
		}
		
		/** @private **/
		protected function clearTip():void {
			try {
				m_timer.reset();
			}catch (e:Error) {
				trace("ToolTip::clearTip.m_timer->", e.message);
			}
			//删除文本框实例
			if (m_tipPanel && m_stage) {
				try {
					m_stage.removeChild(m_tipPanel);
				}catch (e:Error) {
					trace("ToolTip::clearTip.removeChild->", e.message);
				}
				m_tipPanel = null;
			}
		}
		
		/** @private **/
		protected function onTimerHandler(e:TimerEvent):void {
			if (m_tipPanel || (!m_stage) || (m_tipList[m_currTarget] == undefined) || (m_tipList[m_currTarget] == "")) {
				return;
			}
			m_tipPanel = new TextField();
			//显示多行
			m_tipPanel.multiline = true;
			//设置文本
			m_tipPanel.htmlText = m_tipList[m_currTarget];
			//设置文本颜色
			m_tipPanel.textColor = m_fontColor;
			//文本不可选　
			m_tipPanel.selectable = false;
			//允许使用边框颜色
			m_tipPanel.border = true;
			//设置边框颜色
			m_tipPanel.borderColor = m_borderColor;
			//允许使用背景色
			m_tipPanel.background = true;
			//设置背景颜色
			m_tipPanel.backgroundColor = m_bgColor;
			//设置宽度和高度
			if (m_tipPanel.textWidth < m_maxWidth) {
				m_tipPanel.width = m_tipPanel.textWidth + PADDING_NUM;
			}else {
				//自动换行
				m_tipPanel.wordWrap = true;
				m_tipPanel.width = m_maxWidth;
			}
			m_tipPanel.height = m_tipPanel.textHeight + PADDING_NUM;
			setTipLocation();
			m_stage.addChild(m_tipPanel);
		}
		
		/** @private **/
		protected function setTipLocation():void {
			m_tipPanel.x = m_stage.mouseX + MARGIN_X;
			m_tipPanel.y = m_stage.mouseY + MARGIN_Y;
			//如果超出右边界
			if ((m_tipPanel.x + m_tipPanel.width) > m_stage.stageWidth) {
				//设置 tip 在鼠标的左边
				m_tipPanel.x = m_stage.mouseX - m_tipPanel.width - MARGIN_X;
			}
			//判断高度
			if((m_tipPanel.height * 2) < m_stage.stageHeight) {
				//如果超出下边界
				if((m_tipPanel.y + m_tipPanel.height) > m_stage.stageHeight) {
					//设置 tip 在鼠标的上边
					m_tipPanel.y = m_stage.mouseY - m_tipPanel.height - MARGIN_Y;
				}
			}else {
				//居中
				m_tipPanel.y = m_stage.mouseY - m_tipPanel.height / 2;
			}
		}
	}	
}